package com.md.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.md.base.BaseRepositoryImpl;
import com.md.domain.model.po.RoleApiPO;
import com.md.domain.repository.RoleApiRepository;
import com.md.infrastructure.persistence.mapper.RoleApiMapper;
import com.md.util.ObjectUtil;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 刘骄阳
 * 2022-06-12 1:22
 */
@Repository
public class DAORoleApiRepository extends BaseRepositoryImpl<RoleApiPO, RoleApiMapper>
  implements RoleApiRepository {
  public DAORoleApiRepository(final RoleApiMapper mapper) {
    super(mapper);
  }

  @Override
  public Integer checkPermission(final Long userId, final Long apiId) {
    return mapper.checkPermission(userId, apiId);
  }

  @Override
  public void delRoleApiLink(final List<Long> roleIdList, final List<Long> delApiIdList,
                             final List<Long> moduleIdList) {
    if (ObjectUtil.isAllEmpty(delApiIdList, moduleIdList)) {
      return;
    }
    LambdaUpdateWrapper<RoleApiPO> wrapper = new LambdaUpdateWrapper<>();
    wrapper.in(RoleApiPO::getRoleId, roleIdList)
      .in(ObjectUtil.notEmpty(delApiIdList), RoleApiPO::getApiId, delApiIdList)
      .in(RoleApiPO::getModuleId, moduleIdList).set(RoleApiPO::getIsDeleted, 1);
    mapper.update(new RoleApiPO(), wrapper);
  }

  @Override
  public void updateTypeByIdList(final List<Long> updateRoleApiIdList, final Integer type) {
    if (ObjectUtil.isEmpty(updateRoleApiIdList)) {
      return;
    }
    LambdaUpdateWrapper<RoleApiPO> wrapper = new LambdaUpdateWrapper<>();
    wrapper.in(RoleApiPO::getId, updateRoleApiIdList).set(RoleApiPO::getType, type);
    mapper.update(new RoleApiPO(), wrapper);
  }

  @Override
  public List<RoleApiPO> selectRoleApi(final Long roleId) {
    LambdaQueryWrapper<RoleApiPO> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(RoleApiPO::getRoleId, roleId);
    return mapper.selectList(wrapper);
  }
}
